package com.ftg.learn.amqp.helloworld;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * @author 布衣
 */
public class Provider {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要发送的消息：");
        while (true) {
            //基础信息
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("10.2.40.82");
            factory.setPort(5672);
            factory.setUsername("admin");
            factory.setPassword("admin");
            factory.setVirtualHost("/demo");

            //拿到连接
            try (
                    Connection connection = factory.newConnection();
            ) {
                //创建channel
                Channel channel = connection.createChannel();

                channel.exchangeDeclare("myChart", "fanout");
                //    通过频道绑定队列，

                //发送信息
                String word = "康加修说: "+scanner.next();
                //   发送
                channel.basicPublish("myChart", "", null, word.getBytes());
                System.out.println("发送成功");
            } catch (IOException | TimeoutException e) {
                throw new RuntimeException(e);
            }
        }

    }

}
